package cn.it.shop.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.it.shop.service.TemplateMessageService;
import cn.it.shop.util.GlobalConstants;
import cn.it.shop.util.HttpUtils;
import cn.it.shop.wechat.model.templatemessage.Template;
import cn.it.shop.wechat.model.templatemessage.TemplateParam;

/**
 * ClassName: TemplateMessageController
 * 
 * @Description: 模板消息Controller
 * @author
 * @date
 */
@Controller
@RequestMapping("/templateMessage")
public class TemplateMessageController extends BaseController{
	
	
					
	/***
	 * 
	 * @return
	 */
	@RequestMapping("/getAllPrivateTemplate")
	public String getAllPrivateTemplate (HttpServletRequest request){
		//获取模板列表 
		JSONArray array = templateMessageService.getAllPrivateTemplate();
		//获取模板id list
		List<String> templateMessageIdList = templateMessageService.getTemplateId(array);
		List<TemplateParam> templateParamList = new ArrayList<TemplateParam>();
		//设置模板参数对象
		TemplateParam templateParam = new TemplateParam();
		templateParam.setName("user");
		templateParam.setColor("#5C3317");
		templateParam.setValue("吕不韦");
		templateParamList.add(templateParam);
		//设置模板参数对象2
		TemplateParam templateParam2 = new TemplateParam();
		templateParam2.setName("money");
		templateParam2.setColor("#5C3317");
		templateParam2.setValue("157965.00");
		templateParamList.add(templateParam2);
		
		//设置模板对象
		Template template = new Template();
		template.setTemplateId(templateMessageIdList.get(0));//模板id
		template.setToUser(request.getParameter("openid"));//设置接收方id openId
		template.setTopColor("#00FF00");//标题颜色
		template.setUrl("http://www.baidu.com");
		template.setTemplateParamList(templateParamList);
		//发送模板消息
		String result = templateMessageService.sendTemplateMessage(template);
		
		try {
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/audience/index.do"; 
	}
	
	
	/***
	 * 删除消息模板
	 * @return
	 */
	//@ResponseBody
	@RequestMapping("/deleteTemplateMessage")
	public String deleteTemplateMessage (HttpServletRequest request){
		String result = templateMessageService.deleteTemplateMessage("Vl-8uV7phRyUO8L-icu-aVX4TLGW5XZHdyzu9n1Ae1M");
		System.out.println(result);
		return "redirect:/audience/index.do"; 
	}
	
	
	/***
	 * 设置行业信息  
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/setIndustry")
	public String setIndustry (HttpServletRequest request){
		
		return "";
	}
	
	
	
}
